package me.lc.jokealot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import me.lc.jokealot.entity.JokeBody;
import me.lc.jokealot.entity.JokeClassify;
import me.lc.jokealot.entity.JokeItem;
import me.lc.jokealot.util.ShareSettingUtil;
import android.app.Activity;
import android.content.SharedPreferences;

/**
 * 数据管理中心
 * 
 * @author Administrator
 * 
 */
public class DataManager {
	public static DataManager instance = new DataManager();

	private DataManager() {
		classList = new ArrayList<JokeClassify>();
		currentJokeItemList = new ArrayList<JokeItem>();
	}

	public void initData(Activity actvity) {
		SharedSettings = actvity.getPreferences(Activity.MODE_PRIVATE);

		classList.clear();

		JokeClassify class1 = new JokeClassify();
		class1.setClassId(1);
		class1.setClassName("综合笑话");
		classList.add(class1);

		JokeClassify class2 = new JokeClassify();
		class2.setClassId(2);
		class2.setClassName("动物笑话");
		classList.add(class2);

		JokeClassify class3 = new JokeClassify();
		class3.setClassId(3);
		class3.setClassName("校园笑话");
		classList.add(class3);

		JokeClassify class4 = new JokeClassify();
		class4.setClassId(4);
		class4.setClassName("爆笑男女");
		classList.add(class4);		
		
		JokeClassify class5 = new JokeClassify();
		class5.setClassId(5);
		class5.setClassName("职场笑话");
		classList.add(class5);
		
		JokeClassify class6 = new JokeClassify();
		class6.setClassId(6);
		class6.setClassName("名人笑话");
		classList.add(class6);
		
		JokeClassify class7 = new JokeClassify();
		class7.setClassId(7);
		class7.setClassName("冷笑话");
		classList.add(class7);		
		
		JokeClassify class8 = new JokeClassify();
		class8.setClassId(8);
		class8.setClassName("名著暴笑");
		classList.add(class8);	
		
		JokeClassify class9 = new JokeClassify();
		class9.setClassId(9);
		class9.setClassName("短信笑话");
		classList.add(class9);
		
		JokeClassify class10 = new JokeClassify();
		class10.setClassId(10);
		class10.setClassName("儿童笑话");
		classList.add(class10);
		
		JokeClassify class11 = new JokeClassify();
		class11.setClassId(11);
		class11.setClassName("夫妻笑话");
		classList.add(class11);
		
		JokeClassify class12 = new JokeClassify();
		class12.setClassId(12);
		class12.setClassName("社会笑话");
		classList.add(class12);
	}

	private static String categoryHistoryKey = "cat";

	public List<JokeClassify> classList;
	public List<JokeItem> currentJokeItemList;
	public int currentCategoryIndex = 0;
	public String currentCategoryName;
	public SharedPreferences SharedSettings;

	public void loadJokeList(int cIndex, Activity actvity) {
		try {
			int cId = actvity.getResources().getIdentifier("c" + cIndex, "raw",
					actvity.getPackageName());
			InputStream is = actvity.getResources().openRawResource(cId);
			InputStreamReader sr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(sr);
			currentJokeItemList.clear();
			String line = br.readLine();
			int lineIndex = 0;
			while (line != null && !line.equals("")) {
				String[] component = line.split("#");
				JokeItem newItem = new JokeItem();
				newItem.setIndex(lineIndex);
				newItem.setTitle(component[0]);
				currentJokeItemList.add(newItem);
				line = br.readLine();
				lineIndex++;
			}
			br.close();
			sr.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public int getLastJokeIndex() {
		return ShareSettingUtil.getIntSetting(categoryHistoryKey
				+ currentCategoryIndex, 0);
	}

	public void saveLastJokeIndex(int index) {
		ShareSettingUtil.storeIntSetting(categoryHistoryKey
				+ currentCategoryIndex, index);
	}

	public JokeBody loadJokeItem(int cIndex, int lineIndex, Activity actvity) {
		try {
			int cId = actvity.getResources().getIdentifier("c" + cIndex, "raw",
					actvity.getPackageName());
			InputStream is = actvity.getResources().openRawResource(cId);
			InputStreamReader sr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(sr);

			int currIndex = 0;
			String line = br.readLine();
			while (currIndex < lineIndex && line != null && !line.equals("")) {
				line = br.readLine();
				currIndex++;
			}
			br.close();
			sr.close();
			is.close();

			JokeBody body = new JokeBody();
			String[] component = line.split("#");
			body.setTitle(component[0]);
			
			String jokeListStr = component[1];
			jokeListStr = jokeListStr.replace("@@", "\n\n");
			
			body.setJoke(jokeListStr);
			return body;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
